import axios from 'axios'

let myAxios = axios.create({
  baseURL: 'http://localhost:5173/api',
  timeout: 5000,
})

myAxios.interceptors.request.use((config) => {
  config.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
  return config
})

myAxios.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    if (error.response.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/'
    }
  },
)
export default myAxios
